﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lswweb.Domain;

namespace Lswweb.Controls
{
    public class CallBackMultiTextBox : CallbackDropDownList
    {
        public CallBackMultiTextBox()
        {
            this.Rows = 5;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.ClientScript.RegisterStartupScript(typeof(CallbackDropDownList), "Script", RenderScript(), true);
            Page.ClientScript.RegisterHiddenField(this.ClientID, string.Empty);
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            base.Render(writer);

            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.Write("<input onclick=\"AddClick('" + this.ClientID + "')\" type=\"button\" value=\"" + this.Strings["Add"] + "\" />");
            writer.Write("<input onclick=\"RemoveClick('" + this.ClientID +"')\" type=\"button\" value=\"" + this.Strings["Remove"] + "\" />");
            writer.RenderEndTag();
            writer.AddAttribute(HtmlTextWriterAttribute.Size, Rows.ToString());
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "Select");
            if (!string.IsNullOrEmpty(MultiTextBoxStyle))
                writer.AddAttribute(HtmlTextWriterAttribute.Style, MultiTextBoxStyle);
            writer.RenderBeginTag(HtmlTextWriterTag.Select);
            string inputValue = string.Empty;
            if (!string.IsNullOrEmpty(selectPath))
            {
                string[] items = selectPath.Split(',');
                foreach (string listItem in items)
                {
                    string[] label = listItem.Split('-');
                    inputValue += label[1] + "/";
                    writer.Write("<option value=\"" + label[1] + "\">" + label[0] + "</option>");
                }
            }
            writer.RenderEndTag();
            writer.RenderEndTag();
            if (!string.IsNullOrEmpty(inputValue))
                Page.ClientScript.RegisterStartupScript(typeof(CallBackMultiTextBox), this.ClientID + "SetValue", "$(function(){$(\"#" + this.ClientID + "\").val(\"" + inputValue + "\");});", true);
        }

        private string RenderScript()
        {
            StringBuilder script = new StringBuilder();
            script.Append("function AddClick(clientID){");
            script.Append("if($(\"#\" + clientID + \"DropDownList\").val()==\"\"){alert(\"请先选择完整的添加项。\");return;}");
            script.Append("var dropDownList=$(\"#\" + clientID + \"Span > select option:selected\"),text=\"\",value=\"\";");
            script.Append("for(var i=0;i<dropDownList.length;i++){");
            script.Append("if(i==(dropDownList.length-1)){");
            script.Append("text=text + $(dropDownList[i]).text();");
            script.Append("value=$(dropDownList[i]).val();}");
            script.Append("else{text=text + $(dropDownList[i]).text() + \" > \";}}");
            script.Append("if($(\"#\" + clientID + \"Select option\").is(\"[value=\" + value + \"]\"))return;");
            script.Append("$(\"#\" + clientID + \"Select\").append(\"<option value=\" + value + \">\" + text + \"</option>\");");
            script.Append("$(\"#\" + clientID).val($(\"#\" + clientID).val() + value + \"/\");}");
            script.Append("function RemoveClick(clientID){");
            script.Append("$(\"#\" + clientID).val($(\"#\" + clientID).val().replace($(\"#\" + clientID + \"Select option:selected\").val() + \"/\",\"\"));");
            script.Append("$(\"#\" + clientID + \"Select option:selected\").remove();}");
            return script.ToString();
        }

        protected override string DropDownListValueInputId
        {
            get
            {
                return this.ClientID + "DropDownList";
            }
        }

        private string selectPath;
        /// <summary>
        /// 格式 text-value,text-value,
        /// </summary>
        public new string SelectPath
        {
            set
            {
                selectPath = value;
            }
        }

        /// <summary>
        /// 多个值之间用"/"隔开.
        /// </summary>
        public new string SelectValue
        {
            get
            {
                string value = Page.Request.Form[this.ClientID];
                return (string.IsNullOrEmpty(value)) ? string.Empty : value.TrimEnd('/'); 
            }
        }

        public string MultiTextBoxStyle { get; set; }
    }
}
